package co.edu.usbcali.lidis.dataaccess.daoFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import co.edu.usbcali.lidis.dataaccess.conexionbd.ConnectionManager;
import co.edu.usbcali.lidis.modelo.Usuario;

public class FachadaBaseDeDatos {

	private Connection connection;

	private static FachadaBaseDeDatos me = null;

	private FachadaBaseDeDatos() {

	}

	public static FachadaBaseDeDatos getInstance() {
		if (me == null) {
			me = new FachadaBaseDeDatos();
		}

		try {
			if (me.getConnection() == null
					|| me.getConnection().isClosed() == true) {
				me.setConnection(ConnectionManager.getConnection());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return me;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	public Connection getConnection() {
		return connection;
	}

	public boolean iniciarSesion(Usuario usu) throws Exception {
		PreparedStatement pst = null;
		ResultSet rs = null;
		String sql = "SELECT USU_USUARIO, USU_PASSWORD from usuario WHERE USU_USUARIO=? and USU_PASSWORD=?";
		try {
			pst = connection.prepareStatement(sql);
			pst.setString(1, usu.getUsuUsuario());
			pst.setString(2, usu.getUsuPassword());
			rs = pst.executeQuery();
			if (rs.next()) {
				return true;
			}
			return false;
		} catch (Exception e) {
			throw e;
		} finally {
			if (pst != null) {
				pst.close();
			}
		}
	}
}
